package com.geeklan.smart.data.repository.msg;

import androidx.annotation.NonNull;

import com.geeklan.smart.data.model.ApiResponse;
import com.geeklan.smart.data.model.AppProblem;
import com.geeklan.smart.data.model.BannerResponse;
import com.geeklan.smart.data.model.Message;
import com.geeklan.smart.data.model.RequestReadMsg;
import com.geeklan.smart.data.model.VersionResponse;
import com.geeklan.smart.data.remote.RetrofitClient;
import com.geeklan.smart.data.remote.common.CommonApiService;
import com.geeklan.smart.data.repository.RepositoryCallback;

import java.util.List;
import java.util.Map;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class CommonRepository {
    private final CommonApiService apiService;

    public CommonRepository() {
        this.apiService = RetrofitClient.getInstance().getMsgApiService();
    }

    public void getMsgList(Map<String, Integer> options,RepositoryCallback<ApiResponse<List<Message>>> callback) {
        Call<ApiResponse<List<Message>>> call = apiService.getMsgList(options);
        call.enqueue(new Callback<ApiResponse<List<Message>>>() {
            @Override
            public void onResponse(@NonNull Call<ApiResponse<List<Message>>> call, @NonNull Response<ApiResponse<List<Message>>> response) {
                if (response.isSuccessful() && response.body() != null) {
                    if(response.body().getCode() == 0) {
                        callback.onSuccess(response.body());
                    }else {
                        callback.onError(new Exception(response.body().getMsg()));
                    }
                } else {
                    callback.onError(new Exception("Failed to fetch user data")); // 处理错误
                }
            }

            @Override
            public void onFailure(@NonNull Call<ApiResponse<List<Message>>> call, @NonNull Throwable t) {
                callback.onError(new Exception(t)); // 处理错误
            }
        });
    }
    public void getAppVersion(Map<String, Integer> options,RepositoryCallback<ApiResponse<List<VersionResponse>>> callback) {
        Call<ApiResponse<List<VersionResponse>>> call = apiService.getAppVersion(options);
        call.enqueue(new Callback<ApiResponse<List<VersionResponse>>>() {
            @Override
            public void onResponse(@NonNull Call<ApiResponse<List<VersionResponse>>> call, @NonNull Response<ApiResponse<List<VersionResponse>>> response) {
                if (response.isSuccessful() && response.body() != null) {
                    if(response.body().getCode() == 0) {
                        callback.onSuccess(response.body());
                    }else {
                        callback.onError(new Exception(response.body().getMsg()));
                    }
                } else {
                    callback.onError(new Exception("Failed to fetch user data")); // 处理错误
                }
            }

            @Override
            public void onFailure(@NonNull Call<ApiResponse<List<VersionResponse>>> call, @NonNull Throwable t) {
                callback.onError(new Exception(t)); // 处理错误
            }
        });
    }

    public void getAppProblem(Map<String, Integer> options,RepositoryCallback<ApiResponse<List<AppProblem>>> callback) {
        Call<ApiResponse<List<AppProblem>>> call = apiService.getAppProblem(options);
        call.enqueue(new Callback<ApiResponse<List<AppProblem>>>() {
            @Override
            public void onResponse(@NonNull Call<ApiResponse<List<AppProblem>>> call, @NonNull Response<ApiResponse<List<AppProblem>>> response) {
                if (response.isSuccessful() && response.body() != null) {
                    if(response.body().getCode() == 0) {
                        callback.onSuccess(response.body());
                    }else {
                        callback.onError(new Exception(response.body().getMsg()));
                    }
                } else {
                    callback.onError(new Exception("Failed to fetch user data")); // 处理错误
                }
            }

            @Override
            public void onFailure(@NonNull Call<ApiResponse<List<AppProblem>>> call, @NonNull Throwable t) {
                callback.onError(new Exception(t)); // 处理错误
            }
        });
    }

    public void getAppBanner(RepositoryCallback<ApiResponse<BannerResponse>> callback) {
        Call<ApiResponse<BannerResponse>> call = apiService.getAppBanner();
        call.enqueue(new Callback<ApiResponse<BannerResponse>>() {
            @Override
            public void onResponse(@NonNull Call<ApiResponse<BannerResponse>> call, @NonNull Response<ApiResponse<BannerResponse>> response) {
                if (response.isSuccessful() && response.body() != null) {
                    if(response.body().getCode() == 0) {
                        callback.onSuccess(response.body());
                    }else {
                        callback.onError(new Exception(response.body().getMsg()));
                    }
                } else {
                    callback.onError(new Exception("Failed to fetch user data")); // 处理错误
                }
            }

            @Override
            public void onFailure(@NonNull Call<ApiResponse<BannerResponse>> call, @NonNull Throwable t) {
                callback.onError(new Exception(t)); // 处理错误
            }
        });
    }

    public void readMsg(RequestReadMsg requestReadMsg,RepositoryCallback<ApiResponse> callback) {
        Call<ApiResponse> call = apiService.readMsg(requestReadMsg);
        call.enqueue(new Callback<ApiResponse>() {
            @Override
            public void onResponse(@NonNull Call<ApiResponse> call, @NonNull Response<ApiResponse> response) {
                if (response.isSuccessful() && response.body() != null) {
                    if(response.body().getCode() == 0) {
                        callback.onSuccess(response.body());
                    }else {
                        callback.onError(new Exception(response.body().getMsg()));
                    }
                } else {
                    callback.onError(new Exception("Failed to fetch user data")); // 处理错误
                }
            }

            @Override
            public void onFailure(@NonNull Call<ApiResponse> call, @NonNull Throwable t) {
                callback.onError(new Exception(t)); // 处理错误
            }
        });
    }

}
